//
//  SCOrderListController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/6/3.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCOrderListController.h"
#import "SCOrderListCell.h"
#import "SCOrderHeaderView.h"
#import "SCOrderFooterView.h"
#import "SCOrderDetailController.h"
#import "SCOrderEvaluaController.h"

@interface SCOrderListController ()<UITableViewDelegate,UITableViewDataSource,SCOrderFooterViewDelegate>

@property (strong, nonatomic) UITableView    *tableView;
@property (strong, nonatomic) NSMutableArray *dataArray;

@end

@implementation SCOrderListController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)zj_viewDidAppearForIndex:(NSInteger)index{
    //请求数据
    [SVProgressHUD showWithStatus:@"加载中..."];
    [self.dataArray removeAllObjects];
    [self requestData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BGColor;
    // 设置tableView
    [self setupTableView];
}
- (void)setupTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - kTopHeight - 38) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = BGColor;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 102;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 8)];
    self.tableView.tableFooterView = [UIView new];
    // 注册
    [self.tableView registerNib:[UINib nibWithNibName:@"SCOrderListCell" bundle:nil] forCellReuseIdentifier:@"SCOrderListCellID"];
    [self.view addSubview:self.tableView];
}
#pragma mark - 表格代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SCModel *model = self.dataArray[section];
    return model.list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SCOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCOrderListCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SCModel *model = self.dataArray[indexPath.section];
    cell.model = model.list[indexPath.row];
    return cell;
}
#pragma mark - header/footer
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    static NSString *headerID = @"header";
    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerID];
    if (header == nil) {
        header = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:headerID];
        SCOrderHeaderView *headerView = [SCOrderHeaderView addOrderHeaderView];
        headerView.frame = CGRectZero;
        [header addSubview:headerView];
    }
    for (UIView *View in header.subviews) {
        if ([View isKindOfClass:[SCOrderHeaderView class]]) {
            SCOrderHeaderView *headerView = (SCOrderHeaderView *)View;
            headerView.model = self.dataArray[section];
        }
    }
    return header;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    static NSString *footerID = @"footer";
    UITableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:footerID];
    if (footer == nil) {
        footer = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:footerID];
        SCOrderFooterView *footerView = [SCOrderFooterView addOrderFooterView];
        footerView.frame = CGRectZero;
        [footer addSubview:footerView];
    }
    for (UIView *View in footer.subviews) {
        if ([View isKindOfClass:[SCOrderFooterView class]]) {
            SCOrderFooterView *footerView = (SCOrderFooterView *)View;
            footerView.model = self.dataArray[section];
            footerView.tag = section;
            footerView.delegate = self;
        }
    }
    return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 82;
}
#pragma mark - 点击了尾部按钮代理事件
- (void)clickButtonWithSection:(NSInteger)section button:(UIButton *)sender{
    
    SCModel *model = self.dataArray[section];
    
    if ([sender.titleLabel.text isEqualToString:@"查看订单"]) {
        
        SCOrderDetailController *vc = [[SCOrderDetailController alloc]init];
        vc.orderid = model.orderid;
        vc.type = model.type;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([sender.titleLabel.text isEqualToString:@"立即支付"]){
        
    }else if ([sender.titleLabel.text isEqualToString:@"取消订单"]){
        
        [MyTool alertTitle:@"您确定要取消订单?" mesasge:nil preferredStyle:UIAlertControllerStyleAlert confirmHandler:^(UIAlertAction * _Nonnull action) {
            
            NSDictionary *dict = @{@"type":model.type,
                                   @"token":Token,
                                   @"orderid":model.orderid};
            NSLog(@"取消订单提交的数据%@",dict);
            NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftAppOrder&a=App_cancel"];
            [SVProgressHUD showWithStatus:@"取消中..."];
            [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
                [SVProgressHUD showSuccessWithStatus:@"取消成功"];
                [self.dataArray removeObjectAtIndex:section];
                [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:section]                     withRowAnimation:UITableViewRowAnimationAutomatic];
            }];
            
        } cancleHandler:nil viewController:self];
        
    }else if ([sender.titleLabel.text isEqualToString:@"立即评价"]){
        SCOrderEvaluaController *vc = [[SCOrderEvaluaController alloc]init];
        vc.goodsArray = model.list;
        vc.type = model.type;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - 请求数据
- (void)requestData{
    
    NSDictionary *dict = @{@"token":Token,
                           @"status":self.status,
                           @"size":@"10",
                           @"pagenow":@(1)};
    NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftAppOrder&a=App_myorder"];
    //NSLog(@"%@提交的数据%@",Token,dict);
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        NSLog(@"返回的数据:%@",result);
        [SVProgressHUD dismiss];
        NSArray *tempArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"data"]];
        for (SCModel *model in tempArray) {
            model.list = [SCModel mj_objectArrayWithKeyValuesArray:model.list];
            for (SCModel *goodsModel in model.list) {
                goodsModel.type = model.type;
            }
            if ([self.status intValue] != 0) {
                model.status = self.status;
            }else{
                for (SCModel *goodsModel in model.list) {
                    if ([goodsModel.status intValue] == 1 ||
                        [goodsModel.status intValue] == 4 ) {
                        model.status = goodsModel.status;
                        break;
                    }else{
                        model.status = 0;
                    }
                }
            }
        }
        [self.dataArray addObjectsFromArray:tempArray];
        [self.tableView reloadData];
    }];
}

@end
